package com.dayuanit.dy15.lesson29.demo3;

public class ProducerRunnable implements Runnable {

    private MarCarOrder marCarOrder;

    public ProducerRunnable(MarCarOrder marCarOrder) {
        this.marCarOrder = marCarOrder;
    }

    @Override
    public void run() {
        while (true) {
            synchronized (MarCarOrder.obj) {
                create();
            }

            try {
                Thread.sleep(3 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void create() {
        while (marCarOrder.getOrder() != null) {
            try {
                System.out.println(Thread.currentThread().getName() + " 等待...");
                MarCarOrder.obj.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        //生产者要做的业务
        marCarOrder.createOrder("W");
        System.out.println(Thread.currentThread().getName() + " -> " + "W");

        //做完业务后 别忘记通知一下等待在对象上的线程
        MarCarOrder.obj.notifyAll();
    }
}
